package me.raih.controller.user;

import me.raih.pojo.po.Category;
import me.raih.pojo.po.Wares;
import me.raih.pojo.vo.ResultInfo;
import me.raih.service.AppointmentService;
import me.raih.service.CategoryService;
import me.raih.service.WaresService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author Raih
 * @Time 2021/1/25  16:20
 *
 * 用户对商品的一些控制咯
 */
@Controller
@RequestMapping(value = "/user")
public class UserWaresController {

    @Autowired
    WaresService waresService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    AppointmentService appointmentService;

    @Autowired
    ResultInfo resultInfo;

    //商品详细信息页面
    @RequestMapping(value = "/wares_list")
    public String toWaresList(@RequestParam("no")String no,Model model){
        //通过商品的  编码 查询 商品
        Wares wares = waresService.findWaresByNo(no);
        //保存商品传递到页面中
        model.addAttribute("WARES",wares);
        int categoryNo = wares.getCategoryNo();
        Category category = categoryService.findCategoryByNo(categoryNo);
        String name = category.getName();
        //商品的分类一并传递过去
        model.addAttribute("CATEGORY_NAME",name);


        //在这里也要查询是否已经预约了和这件商品一共预约了多少次

        return "user/wares_list";

    }


    //查看商品是否被当前用户预约
    @RequestMapping(value = "/wares/appointment",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo waresAppointment(HttpSession session,@RequestParam("username")String username, @RequestParam("waresNo") String waresNo){
        //从session中获取登录对象
        int i = appointmentService.findAppointmentStatus(username, waresNo);
        //根据结果来判断信息
        if(i == 1){
            //被预约了
            resultInfo.setFlag(true);
        }else if(i == 0){
            //没有被预约
            resultInfo.setFlag(false);
        }else if(i == -1){
            resultInfo.setFlag(false);
            resultInfo.setMessage("出现了错误，请稍后访问");
        }else {
            resultInfo.setFlag(false);
            resultInfo.setMessage("究竟是什么让你来到了这里呢？");
        }
        return resultInfo;
    }

    //改变商品预约状态
    @RequestMapping(value = "/wares/changeAppointment")
    @ResponseBody
    public ResultInfo changeWaresAppointment(@Param("waresNo") String waresNo,@RequestParam("username")String username,HttpSession session){
        int i = appointmentService.changeUserWaresStatus(username, waresNo);
        if(i == 1){
            resultInfo.setFlag(true);
            resultInfo.setMessage("预约成功了！");
        }else if(i == 0){
            resultInfo.setFlag(true);
            resultInfo.setMessage("已取消预约");
        }else {
            resultInfo.setFlag(false);
            resultInfo.setMessage("究竟是什么让你来到了这里呢？");
        }
        return resultInfo;
    }


    //查询该商品被预约了多少次
    @RequestMapping(value = "/wares/waresAppointmentCount",method = RequestMethod.PUT)
    @ResponseBody
    public Integer appointmentCount(@RequestParam("waresNo")String waresNo){

        int count = appointmentService.waresAppointmentCount(waresNo);

        return count;
    }


}
